package com.test.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Main {
	public static final char[] hexs = "0123456789abcdef".toCharArray();
	public static final char[] hexs2 = "0123456789ABCDEF".toCharArray();
	
	
	public static final String[] SDKVERS = {
		"4.2.2", "4.2.2", "4.4.2", "2.3.5", "4.0.4",
		"2.3.6", "4.1.1", "4.1.2", "2.3.7",
		"4.0.3", "2.2.2", "4.0", "4.3",
		"4.4.4", "4.4.2", "4.4.3"
	};
	public static String androidVer() {
		Random random = new Random();
		return SDKVERS[random.nextInt(SDKVERS.length)];
	}
	
	public static String makeMAC() {
		//XX-XX-XX-XX-XX-XX
		char[] mac = new char[17];
		Random random = new Random();
		for(int i=0; i<mac.length; i++) {
			if(i%3 == 2)
				mac[i] = ':';
			else
				mac[i] = hexs[random.nextInt(hexs.length)];
		}
		String ret = new String(mac);
		
		return ret;
	}
	
	public static String makeImei() {
		String imei = "";
		
		char[] c0 = new char[15];
		
		Random random = new Random();
		
		if(random.nextBoolean()) {
			c0[0] = '3'; c0[1] = '5';
		} else {
			c0[0] = '8'; c0[1] = '6';
		}
		
		for(int i=2; i<c0.length; i++) {
			c0[i] = (char) ('0' + random.nextInt(10));
		}
		
		imei = new String(c0);
		
		System.out.println("new imei = " + imei);
		
		return imei;
	}
	
	public static final String[] MODELS = {
		"huawei P6","huawei HN3-U01","huawei H60-L02","huawei MT2-L054G",
		"lenovo K910","lenovo A670T","lenovo A788T","lenovo S658T","samsung I869","samsung I879E",
		"samsung G3819D","samsung G3589W","samsung P729","coolpad 5951","coolpad 7320",
		"coolpad 7620L","coolpad 8297W"
	};
	public static final String[] TYPES = { "K-Touch T619", "HUAWEI Y511-T00", "Lenovo A278t", "三星GT-i9100", "MI 2S", "Lenovo A390t", "vivo S7i(t)", "vivo Y11i T", "R821T", "三星GT-i9220",
			"Lenovo A658t", "Android", "vivo Y15T", "Coolpad 8076", "HMIA5", "MI 2A", "vivo S7t", "Lenovo A308t", "ZTE U807", "Lenovo A288t", "Lenovo A630t", "MI 3", "K-Touch C666t", "Coolpad 7231",
			"三星GT-S5830", "Lenovo A298t", "G_7C", "HUAWEI G520-0000", "HUAWEI C8813", "K-Touch C986t", "联想A60", "CoolPad8020", "CoolPad8070", "MI 2SC", "HUAWEI Y 220T", "Lenovo A789", "R823T",
			"ZTE U817" };
	public static String makeModel() {
		Random random = new Random();
		return MODELS[random.nextInt(MODELS.length)];
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			// 连接SQLite的JDBC
			Class.forName("org.sqlite.JDBC");

			// 建立一个数据库名zieckey.db的连接，如果不存在就在当前目录下创建之
			Connection conn = DriverManager.getConnection("jdbc:sqlite:log4.dll");
			Statement stat = conn.createStatement();
			
//			stat.executeUpdate("create table tbl1(name varchar(20), salary int);"); // 创建一个表，两列
//			stat.executeUpdate("insert into tbl1 values('ZhangSan',8000);"); // 插入数据
//			stat.executeUpdate("insert into tbl1 values('LiSi',7800);");
//			stat.executeUpdate("insert into tbl1 values('WangWu',5800);");
//			stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);");
			
			String user_id = null;
			ResultSet rs = stat.executeQuery("SELECT user_id FROM log_app_install limit 1;"); // 查询数据
			while (rs.next()) { // 将查询到的数据打印出来
				user_id = rs.getString("user_id");
			}
			rs.close();
			
			if(user_id == null)
				throw new NullPointerException("can't get user_id");
			

			final int[] appids = {6000015, 6000019, 6000022, 6000024, 6000028, 6000030, 6000031, 6000213, 6000275, 6000317, 6000334, 6000593, 6016331, 6017436, 6023781};
			
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			Random r = new Random();
			StringBuilder sb = new StringBuilder(512);

			for(int i=0; i<1000; i++) {
				long t = System.currentTimeMillis();
				
				r.setSeed(t);
				
				long pkg_start_time = Long.parseLong(format.format(new Date(t - 60*1000)));
				long start_time = Long.parseLong(format.format(new Date(t)));
				String imei = makeImei();
				String mac = "";
				String androidVer = androidVer();
				String model = makeModel();
				
				int count = 10 + r.nextInt(6);
				for(int j=0; j<count; j++) {
					start_time += (r.nextBoolean()? 1:0);
					
					sb.append("INSERT INTO log_app_install ")
					.append("(user_id, vid, pid, imei, android_ver, mac, sys_mac, model, sys_ver, sys_arch, pkg_id, pkg_start_time, app_id, app_ver, is_valid, status, error_desc, start_time, end_time, sent, log_time) ")
					.append("VALUES('")
					.append(user_id)
					.append("', '', '', ")
					.append('\'').append(imei).append("', ")
					.append('\'').append(androidVer).append("', ")
					.append('\'').append(mac).append("', ")
					.append("'', ")
					.append('\'').append(model).append("', ")
					.append("0, ")
					.append("0, ")
					.append("102, ")
					.append(pkg_start_time).append(", ")
					.append(appids[j]).append(", ")
					.append("0, ")
					.append("1, ")
					.append("0, ")
					.append("'', ")
					.append(start_time).append(", ")
					.append(start_time).append(", ")
					.append("0, ")
					.append(start_time)
					.append(");");

					String sql = sb.toString();
					System.out.println("exec " + sql);

					int ret = stat.executeUpdate(sql);
					
					sb.delete(0, sql.length()-1);
				}
			}

//			stat.executeUpdate("insert into tbl1 values('ZhaoLiu',9100);");
			
//			ResultSet rs = stat.executeQuery("SELECT * FROM log_app_install where sent=1;"); // 查询数据
//			while (rs.next()) { // 将查询到的数据打印出来
//				System.out.print("imei = " + rs.getString("imei") + " "); // 列属性一
//				System.out.println("is_valid = " + rs.getString("is_valid")); // 列属性二
//			}
//			rs.close();
			
			conn.close(); // 结束数据库的连接

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
